<?php
declare (strict_types=1);

namespace app\sysuser\controller;

use app\common\controller\SysBackend;
use app\common\enums\ClassHourStatusEnum;
use app\common\enums\OrganizationStateEnum;
use app\common\model\banxi\core\CoreClassHourDetail as CoreClassHourDetailModel;
use app\common\model\banxi\organization\Organization;
use app\common\model\banxi\student\Student;
use app\common\model\banxi\trainer\Trainer as TrainerModel;
use Throwable;

class Home extends SysBackend
{
    protected array $noNeedLogin = [];
    protected array $noNeedPermission = [];

    /**
     * 管理员登录
     * @return void
     * @throws Throwable
     */
    public function getCourseClassHourSalaryLine(): void
    {
        $endTime = strtotime(date('Y-m-d 23:59:59'));
        $startTime = strtotime(date('Y-m-d 00:00:00', strtotime('-14 days')));
        $coreClassHourDetailModel = new CoreClassHourDetailModel();
        $list = $coreClassHourDetailModel->where([
            ['status', '=', ClassHourStatusEnum::SUCCESS],
            ['payAmount', '>', 0],
            ['createTime', '>=', $startTime],
            ['createTime', '<=', $endTime],
        ])->field('DATE_FORMAT(createTime, "%Y-%m-%d") as x,payType as type,SUM(payAmount) as value')
            ->group('x,type')
            ->select()->toArray();
        $resultList = [];
        $startDate = date('Y-m-d', $startTime);
        $endDate = date('Y-m-d', $endTime);
        $dateRange = [];
        $currentDate = $startDate;
        do {
            $dateRange[] = $currentDate;
            $currentDate = date('Y-m-d', strtotime($currentDate . ' +1 day'));
        } while ($currentDate <= $endDate);
        foreach ($dateRange as $xStr) {
            $dayRows = array_filter($list, function ($row) use ($xStr) {
                return $row['x'] === $xStr;
            });
            $wechatRow = null;
            $aliRow = null;
            foreach ($dayRows as $row) {
                if ($row['type'] == '2') {
                    $wechatRow = $row;
                } else {
                    $aliRow = $row;
                }
            }
            $wechatItem = [
                'type' => '微信',
                'x' => $xStr,
                'value' => $wechatRow ? $wechatRow['value'] : 0,
            ];
            $resultList[] = $wechatItem;
            $aliItem = [
                'type' => '支付宝',
                'x' => $xStr,
                'value' => $aliRow ? $aliRow['value'] : 0,
            ];
            $resultList[] = $aliItem;
        }
        $this->ok('操作成功', $resultList);
    }

    /**
     * 管理员注销
     * @return void
     */
    public function getTjData(): void
    {
        // 获取当前日期和月份
        $dateNow = strtotime(date('Y-m-d')); // 当前日期
        $month = strtotime(date('Y-m'));     // 当前月份

        // 初始化结果对象
        $result = [
            'studentCount' => 0,
            'studentDayCount' => 0,
            'studentMonthCount' => 0,
            'trainerCount' => 0,
            'trainerDayCount' => 0,
            'trainerMonthCount' => 0,
            'organizationCount' => 0,
            'organizationDayCount' => 0,
            'organizationMonthCount' => 0,
        ];

        // 学生统计
        $studentModel = new Student();
        $result['studentCount'] = $studentModel->count();
        $result['studentDayCount'] = $studentModel->where([['createTime', '>=', $dateNow]])->count();
        $result['studentMonthCount'] = $studentModel->where([['createTime', '>=', $month]])->count();

        // 教练统计
        $trainerModel = new TrainerModel();
        $result['trainerCount'] = $trainerModel->count();
        $result['trainerDayCount'] = $trainerModel->where([['createTime', '>=', $dateNow]])->count();
        $result['trainerMonthCount'] = $trainerModel->where([['createTime', '>=', $month]])->count();

        // 机构统计
        $organizationModel = new Organization();
        $oWhere = [
            ['status', '<>', OrganizationStateEnum::E1],
        ];
        $result['organizationCount'] = $organizationModel->where($oWhere)->count();
        $result['organizationDayCount'] = $organizationModel->where($oWhere)->where([['createTime', '>=', $dateNow]])->count();
        $result['organizationMonthCount'] = $organizationModel->where($oWhere)->where([['createTime', '>=', $month]])->count();
        $this->ok('操作成功', $result);
    }
}
